<?php
namespace User\Controller;
use Think\Controller;
use Think\Page;
use User\Model\ActivityModel;
use User\Model\ActivityRegisterModel;

class ActivitydsController extends UserController
{
    public function __construct()
    {
        parent::__construct();
        if (!headers_sent() && // 如果页面头部信息还没有输出
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }
    }

    public function index()
    {
        $cond['status'] = array("NEQ", ActivityModel::STATUS_DELETE);
        $cond['token'] = $this->wxuser['token'];
        $cond['picurl'] = 'activityds';

        $count = M("activity")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("activity")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('act_id desc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function add()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $type = 'add';
            $this->assign('type', $type);
            return $this->display('update');
        }
        if (empty($params['name'])) {
            return $this->my_error("请填写名称");
        }
        $data['name'] = $params['name'];
        $data['token'] = $this->wxuser['token'];
        $data['start_date'] = $params['start_date'];
        $data['end_date'] = $params['end_date'];
        $data['status'] = ActivityModel::STATUS_PENDING;
        $data['picurl'] = 'activityds';
        M("activity")->add($data);
        return $this->my_success("成功");
    }

    public function update()
    {
        $type = I("get.type", "", "");
        $actId = I("get.actId", "", "");
        $params = I("post.", "", "");
        if ($type == 'update' && empty($params)) {
            $activity = M("activity")->where(['act_id' => $actId])->find();
            $this->assign("activity", $activity);
            return $this->display();
        }
        $actId = I("post.actId", "", "");
        if (empty($actId)) {
            return $this->my_error("选择的活动信息有误");
        }
        $data['name'] = $params['name'];
        $data['start_date'] = $params['start_date'];
        $data['end_date'] = $params['end_date'];
        M("activity")->where(['act_id' => $actId])->save($data);
        return $this->my_success("成功");
    }

    public function delete()
    {
        $actId = I("get.actId", "", "");
        $data['status'] = ActivityModel::STATUS_DELETE;
        if (!empty($actId)) {
            M("activity")->where(['act_id' => $actId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function pass()
    {
        $actId = I("get.actId", "", "");
        $data['status'] = ActivityModel::STATUS_ACTIVE;
        if (!empty($actId)) {
            M("activity")->where(['act_id' => $actId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function unpass()
    {
        $actId = I("get.actId", "", "");
        $data['status'] = ActivityModel::STATUS_PENDING;
        if (!empty($actId)) {
            M("activity")->where(['act_id' => $actId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function register()
    {
        $params = I("get.", "", "");
        if (empty($params['actId'])) {
            return $this->redirect("/User/Activityds/index");
        }
        $cond['act_id'] = $params['actId'];
        $cond['status'] = array("NEQ", ActivityRegisterModel::STATUS_DELETE);
        if (!empty($params['status'])) {
            $cond['status'] = $params['status'];
        }
        if (!empty($params['mobile'])) {
            $cond['mobile'] = $params['mobile'];
        }
        $count = M("activity_register")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("activity_register")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('reg_id desc')->select();
        if(!empty($list)){
            foreach ($list as &$value){
                $value['activity'] = '';
                $note = explode(',', $value['note']);
                if(in_array(1, $note)){
                    $value['activity'] .= '总决赛<br>';
                }
                if(in_array(2, $note)){
                    $value['activity'] .= '颁奖典礼';
                }
            }
        }
        $this->assign("list", $list);
        $this->display();
    }

    public function registerAdd()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $type = 'add';
            $this->assign('type', $type);
            return $this->display('registerUpdate');
        }
        if (empty($params['name'])) {
            return $this->my_error("请填写姓名");
        }
        if (empty($params['act_id'])) {
            return $this->my_error("请选择相应活动");
        }
        if (empty($params['company'])) {
            return $this->my_error("请填写公司名称");
        }
        if (empty($params['mobile'])) {
            return $this->my_error("请填写申请人手机号码");
        }
        if (empty($params['position'])) {
            return $this->my_error("请填写职位");
        }
        if (empty($params['note'])) {
            return $this->my_error("请选择参与活动");
        }
        $data['name'] = $params['name'];
        $data['token'] = $this->wxuser['token'];
        $data['act_id'] = $params['act_id'];
        $data['company'] = $params['company'];
        $data['position'] = $params['position'];
        $data['mobile'] = $params['mobile'];
        $data['note'] = implode(',' , $params['note']);
        $data['status'] = ActivityRegisterModel::STATUS_ACTIVE;
        M("activity_register")->add($data);
        return $this->my_success("成功");
    }

    public function registerUpdate()
    {
        $type = I("get.type", "", "");
        $regId = I("get.regId", "", "");
        $params = I("post.", "", "");
        if ($type == 'update' && empty($params)) {
            $activityRegister = M("activity_register")->where(['reg_id' => $regId])->find();
            $note = explode(',', $activityRegister['note']);
            $this->assign("activityRegister", $activityRegister);
            $this->assign("note", $note);
            $this->assign("type", $type);
            return $this->display();
        }
        $regId = I("post.regId", "", "");
        if (empty($regId)) {
            return $this->my_error("选择的活动信息有误");
        }
        if (empty($params['name'])) {
            return $this->my_error("请填写姓名");
        }
        if (empty($params['act_id'])) {
            return $this->my_error("请选择相应活动");
        }
        if (empty($params['company'])) {
            return $this->my_error("请填写公司名称");
        }
        if (empty($params['mobile'])) {
            return $this->my_error("请填写申请人手机号码");
        }
        if (empty($params['position'])) {
            return $this->my_error("请填写职位");
        }
        if (empty($params['note'])) {
            return $this->my_error("请选择参与活动");
        }
        $data['name'] = $params['name'];
        $data['token'] = $this->wxuser['token'];
        $data['act_id'] = $params['act_id'];
        $data['company'] = $params['company'];
        $data['position'] = $params['position'];
        $data['mobile'] = $params['mobile'];
        $data['note'] = implode(',', $params['note']);
        $data['card_number'] = $params['card_number'];
        M("activity_register")->where(['reg_id' => $regId])->save($data);
        return $this->my_success("成功");
    }

    public function registerDelete()
    {
        $regId = I("get.regId", "", "");
        $data['status'] = ActivityRegisterModel::STATUS_DELETE;
        if (!empty($regId)) {
            M("activity_register")->where(['reg_id' => $regId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function registerPass()
    {
        $regId = I("get.regId", "", "");
        $data['status'] = ActivityRegisterModel::STATUS_ACTIVE;
        if (!empty($regId)) {
            M("activity_register")->where(['reg_id' => $regId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function registerUnpass()
    {
        $regId = I("get.regId", "", "");
        $data['status'] = ActivityRegisterModel::STATUS_PENDING;
        if (!empty($regId)) {
            M("activity_register")->where(['reg_id' => $regId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function download()
    {

        $params = I("get.", "", "");
        if (empty($params['actId'])) {
            return $this->redirect("/User/Activityds/index");
        }
//        header("Content-type:application/octet-stream");
        header("Accept-Ranges:bytes");
        header("Content-type:application/vnd.ms-excel;charset=UTF-8");
        header("Content-Disposition:attachment;filename=" . "活动注册名单" . date("Y-m-d") . ".xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        echo "<table border='1px;'><tr>";
        $title = [ "姓名", "电话", "公司", "职位", "参与活动" ];
        foreach ($title as $k => $v) {
            $title[$k] = iconv("UTF-8", "GB2312", $v);
            echo "<td>" . iconv("UTF-8", "GB2312", $v) . "</td>";
        }
        echo "</tr>";
//        $title = implode("\t", $title);
//        echo "$title\n";
        $cond['act_id'] = $params['actId'];
        $cond['status'] = array("NEQ", ActivityRegisterModel::STATUS_DELETE);
        if (!empty($params['status'])) {
            $cond['status'] = $params['status'];
        }
        if (!empty($params['mobile'])) {
            $cond['mobile'] = $params['mobile'];
        }
        $count = M("activity_register")->where($cond)->count();
        $number = $count / 100;
        for ($i = 0; $i <= $number; $i++) {
            $list = M("activity_register")->where($cond)->limit($i * 100, 100)->order('reg_id desc')->select();
            $this->exportexcel($list);
        }
        echo "</table>";
    }

    private function exportexcel($data = array())
    {
        //导出xls 开始
        if (!empty($data)) {
            foreach ($data as $key => $val) {
                echo "<tr>";
                echo "<td>" . iconv("UTF-8", "GB2312", $val['name']) . "</td>";
                echo "<td>" . iconv("UTF-8", "GB2312", $val['mobile']) . "</td>";
                echo "<td>" . iconv("UTF-8", "GB2312", $val['company']) . "</td>";
                echo "<td>" . iconv("UTF-8", "GB2312", $val['position']) . "</td>";
                $note = explode(',', $val['note']);
                $activity = '';
                if(in_array(1, $note)){
                    $activity = '总决赛  ';
                }
                if(in_array(2, $note)){
                    $activity .= '颁奖典礼';
                }
                echo "<td>" . iconv("UTF-8", "GB2312", $activity) . "</td>";
                echo "</tr>";
            }
        }
    }
}

?>
